package com.example.schedulecard;

import androidx.annotation.LongDef;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.schedulecard.DataTransfer.ScheduleNameTransfer;
import com.example.schedulecard.Databases.ScheduleFestOption;

public class UpdataScheduleInfo extends AppCompatActivity {
    Button back, enable;
    Spinner daynum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_schedule_info);
        Init();
    }
    public void Init(){
        daynum = (Spinner) findViewById(R.id.updata_schedule_info);
        back = (Button) findViewById(R.id.back_schedule_list);
        enable = (Button) findViewById(R.id.enable_set);
//        点击取消的事件处理
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        点击确定的事件处理
        enable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = Integer.parseInt(daynum.getSelectedItem().toString());
                ScheduleNameTransfer.scheduleinfo.setWorksum(id);
//                修改该课表的天数
                SQLiteDatabase sqLiteDatabase = ScheduleFestOption.CreateScheduletable(
                        UpdataScheduleInfo.this).getWritableDatabase();
                int updataSchedulenum = ScheduleFestOption.updataSchedule(sqLiteDatabase, ScheduleNameTransfer.scheduleinfo);
                if (updataSchedulenum > 0){
                    Log.d("保存是否成功","成功" + ScheduleNameTransfer.scheduleinfo);
//                跳转
                    Toast.makeText(UpdataScheduleInfo.this, "设置成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UpdataScheduleInfo.this, ScheduleCardHome.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(UpdataScheduleInfo.this, "设置失败，请重新设置", Toast.LENGTH_SHORT).show();
                    Log.d("保存是否成功","失败" + ScheduleNameTransfer.scheduleinfo);
                    Intent intent = new Intent(UpdataScheduleInfo.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }
        });

    }
}